package com.googlecode.bipol.war.services;

import java.io.Serializable;
import java.util.List;

import com.googlecode.bip.dao.DAOFactory;
import com.googlecode.bip.entity.ITopic;

public class TopicManagerImpl implements TopicManager, Serializable {
    private static final long serialVersionUID = 1L;

    @Override
    public ITopic createTopic(String name, String description, String idLocal) throws ServiceException {
        // Check that topic does not exist yet
        ITopic t;

        try {
            t = DAOFactory.getDAOFactory().getTopicDAO().findTopic(name, idLocal);
            }
        catch( Exception e ) {
            throw new ServiceException("Error when saving topic: Unknown reason (please contact the administrator)", e);
            }
        if( t != null ) {
            throw new ServiceException("Error when saving topic: the topic with name '"+name+"' and local '"+idLocal+"' already exists !");
            }

        int parentIdTopic = 0; // TODO ! (0 == root)

        // Insert the new topic
        try {
            t = DAOFactory.getDAOFactory().getTopicDAO().insertTopic( parentIdTopic, idLocal, name, description );
            }
        catch(Exception e) {
            throw new ServiceException("Error when saving topic: "+e.getMessage(), e);
            }

        if( t != null ) {
            return t;
            }
        else {
            throw new ServiceException("Error when saving topic: Unknown reason (please contact the administrator)");
            }
    }
    
    public ITopic postTopic(ITopic topic) throws ServiceException {
    	
    	if (topic.getIdTopic() > 0) {
    		try {
    			if (DAOFactory.getDAOFactory().getTopicDAO().updateTopic(topic)) {
    				return topic;
    			}
            }
    		catch(Exception e) {
                throw new ServiceException("Error when updating topic: "+e.getMessage(), e);
            }
    		throw new ServiceException("Error when updating topic: Unknown reason (please contact the administrator)");
    	} else {
    		// Check a topic with the same name and idlocal does not exist yet
            ITopic t;
            try {
                t = DAOFactory.getDAOFactory().getTopicDAO().findTopic(topic.getName(), topic.getIdLocal());
                }
            catch( Exception e ) {
                throw new ServiceException("Error when creating topic: Unknown reason (please contact the administrator)", e);
                }
            if( t != null ) {
                throw new ServiceException("Error when creating topic: the topic with name '"+topic.getName()+"' and local '"+topic.getIdLocal()+"' already exists !");
            }
    		// Insert the new topic
    		int parentIdTopic = 0; // TODO ! (0 == root)
            try {
                t = DAOFactory.getDAOFactory().getTopicDAO().insertTopic( parentIdTopic, topic.getIdLocal(), topic.getName(), topic.getDescription() );
                }
            catch(Exception e) {
                throw new ServiceException("Error when creating topic: "+e.getMessage(), e);
                }

            if( t != null ) {
                return t;
                }
            else {
                throw new ServiceException("Error when creating topic: Unknown reason (please contact the administrator)");
                }
    	}
    }

    @Override
    public ITopic removeTopic(int idtopic, String idlocal) throws ServiceException {
        // Check that topic exists
        ITopic t = ServicesHelper.getExistingTopic(idtopic, idlocal);

        // Delete topic
        try {
            // FIXME WARN: Remove ONLY translation topic part, not topic support (id already exist) see deleteTopic( id )
            if (DAOFactory.getDAOFactory().getTopicDAO().deleteTopicLocal(t.getIdTopic(),t.getIdLocal())) {
                return t;
                }
            }
            catch(Exception e) {
                throw new ServiceException("Error when deleting topic: "+e.getMessage(), e);
            }
        throw new ServiceException("Error when deleting topic: Unknown reason (please contact the administrator)");
    }

    @Override
    public List<ITopic> getAllTopics() throws ServiceException {
        try {
            List<ITopic> topics = DAOFactory.getDAOFactory().getTopicDAO().getAllTopics();
            return topics;
        } catch(Exception e) {
            throw new ServiceException("Error when getting topics: "+e.getMessage(), e);
        }
    }

}
